<?php
declare(strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Setting extends Model
{
    protected $schema = [
        'id'           => 'int',
        'name'         => 'string',
        'value'        => 'string',
        'update_time'  => 'timestamp',
        'created_time' => 'timestamp',
    ];

    public function getById($id)
    {
        $setting = self::findOrEmpty($id);
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function findByIds(array $ids)
    {
        $settings = self::select($ids);
        if ($settings->isEmpty()) {
            return [];
        }
        return $settings->toArray();
    }

    public function getByName($name)
    {
        $setting = self::where('name', $name)->findOrEmpty();
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }

    public function getByFields($field, $value)
    {
        $setting = self::where($field, $value)->findOrEmpty();
        if (empty($setting)) {
            return [];
        }
        return $setting->toArray();
    }
}
